/*
 * @Author: your name
 * @Date: 2021-08-24 15:31:36
 * @LastEditTime: 2021-08-26 21:47:40
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \bigdate\src\store\index.ts
 */ 
// 导入模块 
import { Menu, RoleEnum,State } from '@/routers/type';
import {router} from '@/routers/router';
import LAYOUT from '@/layout/index.vue';
import NOTFOUND  from '@/views/home/404.vue';
import LOGIN from '@/views/home/login.vue';
import REGISTER from '@/views/home/register.vue'
import { createStore } from 'vuex'; 
import { defineAsyncComponent } from 'vue'; 
export const key  = Symbol();
export const store = createStore<State>({
    state() {
        return {
            userinfo:{},
            Menulist:[],
            baseicRouters:[
                 {
                    path:'/',
                    name:'root', 
                    redirect:'/home' 
                 },  
                 {
                    path:'/:path(.*)*',
                    name:'404',
                    component:NOTFOUND, 
                  },
                  {
                    path:'/login',
                    name:'login',
                    component:LOGIN, 
                  },
                  {
                    path:'/register',
                    name:'register',
                    component:REGISTER, 
                  } 
            ],
            commitRouter:[
                {
                    path:'/home',
                    name:'home',
                    mate:{title:'工作台',router:'/home'},
                    meta:{title:'工作台',router:'/home'},
                    isMenu:true,
                    component:LAYOUT,
                    children:[
                        {
                            path:'',
                            name:'homepage',
                            meta:{title:'工作台',router:'/home'},
                            component:defineAsyncComponent(()=>import('@/views/admin/home/index.vue'))
                         },
                    ]
                 },
            ],
            // 页面路由
            routerArr: [
                 {
                   path:'/Economics',
                   name:'Economics',
                   mate:{title:'任务日报',router:'/Economics'},
                   meta:{title:'任务日报',router:'/Economics'},
                   rolus:[RoleEnum.SUPER],
                   url:LAYOUT,
                   isMenu:true, 
                   children:[
                       {
                        path:'',
                        name:'Employmentw',
                        mate:{title:'经济1',router:'/Economics'},
                        meta:{title:'经济1',router:'/Economics'},
                        rolus:[RoleEnum.SUPER], 
                        url:'Economics/index',
                       },
                       
                   ]
                 },
                 {
                    path:'/society',
                    name:'society',
                    mate:{title:'工作日程',router:'/society'},
                    meta:{title:'工作日程',router:'/society'},
                    rolus:[RoleEnum.SUPER,RoleEnum.TEST],
                    url:LAYOUT, 
                    isMenu:true, // 代表单标签
                    children:[
                        {
                            path:'',
                            name:'comprehensive',
                            mate:{title:'综合',router:'/society'},
                            meta:{title:'综合',router:'/society'}, 
                            rolus:[RoleEnum.SUPER,RoleEnum.TEST],
                            url:'society/comprehensive',
                        } 
                    ]
                   }  
            ],
        }
    },
    getters:{
         GetMenulist(state){
             return [...state.commitRouter,...state.Menulist]
         }
    },
    mutations: {
        // 整体页面需要等 路由获取到之后才能进入 
        /**
         * 
         * @param state 
         * @param val 过滤值
         */
        FilterRouter(state, val){
            state.Menulist = [];
            let lists = MapRouter(state.routerArr,val);  
            
            setTimeout(()=>{   
                state.Menulist = lists
                 let cacheRouter = sessionStorage.getItem('ROUTER_PATH');
                lists.forEach(item=>{  
                    router.addRoute(item as any)
                }) 
                router.push(cacheRouter as any)
            },10)
        },
        Cleardatas(state){
            state.Menulist = [];
            state.userinfo = {};
            sessionStorage.clear();
            sessionStorage.setItem('ROUTER_PATH','/');
            sessionStorage.setItem('CREATE_ROUTER','');
            // resetRouter(); 
            router.push('/login');  
        }
    },
    actions:{
        CREATE_ROUTER({commit},val){  
             
              sessionStorage.setItem('CREATE_ROUTER',val); 
              commit('FilterRouter',val); 
        },
        // 退出登录
        LOGIN_OUT({commit}){
               commit('Cleardatas')
        }, 
    },
    modules: {
        
    },
}) 
let modules = import.meta.glob('../views/**/*.vue')
const MapRouter = (routerArr:Menu[],rolu:string)=>{
 
   let result = [] as Menu[] 
   routerArr.forEach((item)=>{
            let {path,mate,name,meta,isMenu,rolus,url,children,redirect} = item as any;
            if(isRolus(rolus,rolu)){
                // console.log(modules[`../views/admin/${url}.vue`],`../views/admin/${url}.vue`)
                  let obj = {
                      path: path,
                      name,
                      mate,
                      meta,
                      isMenu,
                      component:typeof url === 'string' ? modules[`../views/admin/${url}.vue`]:url,
                      children:children?MapRouter(children,rolu):[],
                      redirect
                  }
                  result.push(obj);   
            }
    })  
  return result;
}

const isRolus = (list:RoleEnum[],item:string)=>{
 
     return list.includes(item as string);
}

